<?php
namespace app\model;

use app\common\model\_Base;

class Config extends _Base
{
    
    const VALUE_TYPE_NUMBER = 'number';
    const VALUE_TYPE_STRING = 'string';
    const VALUE_TYPE_ARRAY = 'array';
    const VALUE_TYPE_JSON = 'json';
    const VALUE_TYPE = [
        self::VALUE_TYPE_NUMBER,
        self::VALUE_TYPE_STRING,
        self::VALUE_TYPE_ARRAY,
        self::VALUE_TYPE_JSON,
    ];
    /**
     * 保存数据
     * @param  array $data 数据
     * @return bool       
     */
    public static function doSave($data,$id = '')
    {   
        
        $config = $id?Config::get($id):new Config;
        !empty($data['name']) && $config['name'] = $data['name'];
        !empty($data['alias']) && $config->alias = $data['alias'];
        isset($data['desc']) && $config->desc = $data['desc'];
        !empty($data['type']) && $config['type'] = $data['type'];
        isset($data['sort']) && strlen($data['sort']) > 0 && $config->sort = $data['sort'];
        isset($data['value']) && $config->value = $data['value'];
        if(!empty($config['type']) && in_array($config['type'], self::VALUE_TYPE) && isset($data['value'])){
            switch ($config['type']) {
                case self::VALUE_TYPE_NUMBER:
                    if(!is_numeric($data['value'])){
                        return '值类型错误';
                    }else{
                        $config->value = $data['value'];
                    }
                    break;
                case self::VALUE_TYPE_STRING:
                    if(!is_string($data['value'])){
                        return '值类型错误';
                    }else{
                        $config->value = $data['value'];
                    }
                    break;
                case self::VALUE_TYPE_ARRAY:
                    if(!is_array($data['value'])){
                        return '值类型错误';
                    }else{
                        $config->value = serialize($data['value']);
                    }
                    break;
                case self::VALUE_TYPE_JSON:
                    if(!is_json($data['value'])){
                        return '值类型错误';
                    }else{
                        $config->value = serialize(json_decode($data['value'],1));
                    }
                    break;
            }
        }
        
        $config->save();
        return false;
    }

    /**
     * 删除数据 软删除 二次真实删除
     * @param  integer $id 数据编号
     * @return bool       
     */
    public static function doDelete($id)
    {
        $config = Config::destroy($id);
        return false;
    }

    /**
     * 获取数据
     * @param  array    $cond  条件
     * @param  string   $alias 条件表别名
     * @param  integer  $page  页码
     * @param  integer  $size 每页条数
     * @return array
     */
    public static function doList($cond = [], $page = 1, $size = 10, $order = 'sort')
    {   
        $total = Config::where($cond)->count(); 

        $list = Config::where($cond)->page($page,$size)->order($order)->select();
        if(!$list->isEmpty()){
            foreach ($list as $key => &$value) {
                
                if($value['type'] == self::VALUE_TYPE_ARRAY || $value['type'] == self::VALUE_TYPE_JSON){
                    $value->value = unserialize($value->value);
                }
            }
        }
        return ['total' => $total,'list' => $list];
    }
}